/*
*  This file is part of mangusta
*
*  mangusta is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  mangusta is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with mangusta; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
*
*  Author: <antonino.calderone@ericsson.com>, <acaldmail@gmail.com>
*
*/


/* -------------------------------------------------------------------------- */

#include "nu_about.h"
#include "nu_variant.h"
#include "nu_functions.h"
#include "nu_os_console.h"
#include "nu_os_std.h"
#include "nu_builtin_help.h"
#include "nu_lang_defs.h"

#include <functional>
#include <ctime>
#include <string>
#include <cstdlib>


/* -------------------------------------------------------------------------- */

namespace nu
{


/* -------------------------------------------------------------------------- */

#define THIS_MODULE "std"
#define MD_NAMESPACE THIS_MODULE "."

/* -------------------------------------------------------------------------- */

static struct module_stdlib_t : public module_t
{
   module_stdlib_t()
   {
      add_module(this, THIS_MODULE);
   }

   virtual bool load() throw() override
   {
      auto & hmap = builtin_help_t::get_instance();

      hmap[ {lang_item_t::MODULE, THIS_MODULE}] =
      {
         "The " THIS_MODULE " module provides access to "
         "the standard functions",
         THIS_MODULE" module"
      };

      functions_t & fmap = functions_t::get_instance();

      struct _input_str
      {
         std::string operator()(int n) throw()
         {
            return _os_input_str(n);
         }
      };

      fmap[MD_NAMESPACE "input"] = functor_string_int < _input_str > ;

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "input"}] =
      {
         "Returns a string of n characters read "
         "from the standard input",
         PROGLANG_TYPE_STRING " " MD_NAMESPACE "input(int n)"
      };

      auto functor_errno = [](
                              rt_prog_ctx_t & ctx,
                              const std::string& name,
                              const nu::func_args_t & args)
      {
         functions_t::check_arg_num(args, 0, name);
         return nu::variant_t(ctx.get_errno());
      };

      fmap[MD_NAMESPACE "errno"] = functor_errno;

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "errno"}] =
      {
         "Returns error code of last operation",
         PROGLANG_TYPE_INTEGER " " MD_NAMESPACE "errno()"
      };


      auto functor_errno_str = [](
                                  rt_prog_ctx_t & ctx,
                                  const std::string& name,
                                  const nu::func_args_t & args)
      {
         std::vector< variant_t > vargs;
         functions_t::get_functor_vargs(
            ctx, name, args, { variant_t::type_t::INTEGER }, vargs);

         return nu::variant_t(std::string(strerror(vargs[0].to_int())));
      };

      fmap[MD_NAMESPACE "errnostr"] = functor_errno_str;

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "errnostr"}] =
      {
         "Returns error string describing error errnum",
         PROGLANG_TYPE_STRING " " MD_NAMESPACE "errnostr(int errornum)"
      };


      auto functor_inkeys = [](
                               rt_prog_ctx_t & ctx,
                               const std::string& name,
                               const nu::func_args_t & args)
      {
         functions_t::check_arg_num(args, 0, name);

         int ch = _os_kbhit();
         std::string s;

         if (ch)
         {
            s.push_back(ch);
         }

         return variant_t(s, variant_t::type_t::STRING);
      };

      fmap[MD_NAMESPACE "inkey"] = functor_inkeys;

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "inkey"}] =
      {
         "Reads a keyboard character from the standard input buffer"
         ", without echoing it to the screen. "
         "This function will return immediately, even if there are no character"
         " to retrieve",
         PROGLANG_TYPE_STRING " " MD_NAMESPACE "inkey()"
      };


      struct _getenv_str
      {
         std::string operator()(const std::string& x) throw()
         {
            std::string ret;

            char * var = ::getenv(x.c_str());

            if (var)
            {
               ret = var;
            }

            return ret;
         }
      };

      fmap[MD_NAMESPACE "getenv"] = functor_string_string < _getenv_str > ;

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "getenv"}] =
      {
         "Gets an environment variable",
         PROGLANG_TYPE_STRING " " MD_NAMESPACE "getenv(string var)"
      };


      struct _setenv_str
      {
         int operator()(const std::string& var, const std::string& val) throw()
         {
            return _os_setenv(var.c_str(), val.c_str());
         }
      };

      fmap[MD_NAMESPACE "setenv"] = functor_int_string_string < _setenv_str > ;


      struct _shell
      {
         int operator()(const std::string& var) throw()
         {
            return ::system(var.c_str());
         }
      };

      fmap[MD_NAMESPACE "shell"] = functor_int_string < _shell > ;


      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "setenv"}] =
      {
         "Sets an environment variable to a given value",
         PROGLANG_TYPE_INTEGER " " MD_NAMESPACE "setenv(string var, string val)"
      };


      struct _unsetenv_str
      {
         int operator()(const std::string& var) throw()
         {
            return _os_unsetenv(var.c_str());
         }
      };

      fmap[MD_NAMESPACE "unsetenv"] = functor_int_string < _unsetenv_str > ;

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "unsetenv"}] =
      {
         "Unsets an environment variable",
         PROGLANG_TYPE_INTEGER " " MD_NAMESPACE "unsetenv(string var)"
      };

      auto functor_plat_id = [](
                                rt_prog_ctx_t & ctx,
                                const std::string& name,
                                const nu::func_args_t & args)
      {
         functions_t::check_arg_num(args, 0, name);
#ifdef WIN32
         return 1;
#else // assuming Linux (TODO modify to add other platforms)
         return 2;
#endif
      };

      fmap[MD_NAMESPACE "getplatid"] = functor_plat_id;

      hmap[ {lang_item_t::FUNCTION, "getplatid"}] =
      {
         "Returns 1 for Windows, 2 for other (Linux) platforms\n",
         PROGLANG_TYPE_INTEGER " " MD_NAMESPACE "getplatid()"
      };


      auto functor_ver = [](
                            rt_prog_ctx_t & ctx,
                            const std::string& name,
                            const nu::func_args_t & args)
      {
         functions_t::check_arg_num(args, 0, name);
         return nu::variant_t(std::string(about::version));
      };

      fmap[MD_NAMESPACE "ver"] = functor_ver;

      hmap[ {lang_item_t::FUNCTION, MD_NAMESPACE "ver"}] =
      {
         "Returns mangusta version",
         PROGLANG_TYPE_STRING " " MD_NAMESPACE "ver()"
      };


      return true;
   }


   /* -------------------------------------------------------------------------- */

} module_instance;


/* -------------------------------------------------------------------------- */

} // nu namespace